<?php
/**
 * Created by PhpStorm.
 * User: eeo
 * Date: 2017/6/3
 * Time: 13:53
 */

namespace App\Http\Controllers\Marketing;

use App\Model\Marketing\Agency;
use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;

use App\Http\Controllers\MessagePacket;

class AgencyController extends BaseController
{
    use MessagePacket;

    public function get($id)
    {
        $id = intval($id);
        $obj = Agency::find($id);
        if(empty($obj)){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);
    }

    function gets(Request $request)
    {
        if($request->has('status')){
            $status = $request->get('status');
            $query =  Agency::where('status', $status);
            $data = $query->get();
        }
        else{
            $data = Agency::all();
        }

        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }

    function getOne(){
        $query =  Agency::where('status', 1);
        $data = $query->first();
        if(empty($data)){
            return $this->fail($data, '数据不存在');
        }
        return $this->success($data);
    }

    public function add(Request $request){
        $obj = new Agency($request->all());
        if(!$obj->save()){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);

    }

    public function update($id,Request $request){

        $obj = Agency::find($id);
        $obj->update($request->all());
        if(!$obj->save()){
            return $this->fail($obj, '数据不存在');
        }
        return $this->success($obj);
    }

    public function  delete($id){
        if(Agency::destroy($id)){
            return $this->success(true);
        }
        return $this->fail(false,'删除失败');
    }
}
